{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "import sympy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[0., 1.],\n",
       "         [2., 3.]],\n",
       "\n",
       "        [[4., 5.],\n",
       "         [6., 7.]]])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = torch.arange(8).reshape(2,2,2).to(torch.float32)\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[0., 4.],\n",
       "         [1., 5.]],\n",
       "\n",
       "        [[2., 6.],\n",
       "         [3., 7.]]])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x.permute(1,2,0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[4.0000, 5.0990],\n",
       "        [6.3246, 7.6158]])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.norm(x, p=2, dim=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[4.0000, 5.0990],\n",
       "        [6.3246, 7.6158]])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.norm(torch.tensor([[[0,4],[1,5]], [[2,6],[3,7]]],dtype=torch.float32), p=2, dim=2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 测试torch接口"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[[-0.7181,  0.2476],\n",
      "         [ 1.5442, -0.7237]],\n",
      "\n",
      "        [[ 0.9004, -0.8940],\n",
      "         [ 1.2657,  0.8902]]])\n",
      "tensor([[[2.7367]]])\n",
      "tensor(2.7367)\n"
     ]
    }
   ],
   "source": [
    "a = torch.randn((2, 2, 2))\n",
    "print(a)\n",
    "p2 = torch.norm(a, p=2, keepdim=True)\n",
    "p2_custom = torch.sqrt(torch.sum(torch.abs(a)**2))\n",
    "print(p2)\n",
    "print(p2_custom)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[[-0.7181,  0.2476],\n",
      "         [ 1.5442, -0.7237]],\n",
      "\n",
      "        [[ 0.9004, -0.8940],\n",
      "         [ 1.2657,  0.8902]]])\n",
      "tensor([[0.7595, 1.7054],\n",
      "        [1.2688, 1.5474]])\n",
      "tensor(0.7595)\n"
     ]
    }
   ],
   "source": [
    "p2_dim0 = torch.norm(a, p=2, dim=2)\n",
    "p2_custom_dim0 = torch.sqrt(torch.sum(torch.abs(a[0,0,...])**2))\n",
    "print(a)\n",
    "# print(a.permute(1,2,0))\n",
    "print(p2_dim0)\n",
    "print(p2_custom_dim0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[0.9044, 2.0844]])\n",
      "tensor(0.9044)\n"
     ]
    }
   ],
   "source": [
    "p_inf = torch.norm(a, p=float('inf'), dim=0, keepdim=True)\n",
    "p_info_custom = torch.max(torch.abs(a[...,0]))\n",
    "print(p_inf)\n",
    "print(p_info_custom)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[0.5964, 0.6284]])\n",
      "tensor(0.5964)\n"
     ]
    }
   ],
   "source": [
    "p_ninf = torch.norm(a, p=float('-inf'), dim=0, keepdim=True)\n",
    "p_ninf_custom = torch.min(torch.abs(a[...,0]))\n",
    "print(p_ninf)\n",
    "print(p_ninf_custom)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[2., 2.]])\n",
      "tensor(2)\n"
     ]
    }
   ],
   "source": [
    "p_zero = torch.norm(a, p=0, dim=0, keepdim=True)\n",
    "p_zero_custom = torch.sum(a[...,0] != 0)\n",
    "print(p_zero)\n",
    "print(p_zero_custom)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[1.5008, 2.7129]])\n",
      "tensor(1.5008)\n"
     ]
    }
   ],
   "source": [
    "p_one = torch.norm(a, p=1, dim=0, keepdim=True)\n",
    "p_one_custom = torch.sum(torch.abs(a[...,0]))\n",
    "print(p_one)\n",
    "print(p_one_custom)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[0.9837, 2.1033]])\n",
      "tensor(0.9837)\n"
     ]
    }
   ],
   "source": [
    "p_other = torch.norm(a, p=3, dim=0, keepdim=True)\n",
    "p_other_custom = torch.sum(torch.abs(a[...,0]) ** 3) ** (1/3)\n",
    "print(p_other)\n",
    "print(p_other_custom)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# sympy分析如何计算开方"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$$a^x=e^{xlna}$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = sympy.Symbol('x')\n",
    "p = sympy.Symbol('p')\n",
    "sympy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.19"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
